﻿using System.Collections.Generic;
using Assignment_2.Entities.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Assignment_2.Entities
{
    public class Entity
    {
        public string Name { get; set; }

        public Vector3 Position { get; set; }
        public Matrix Rotation { get; set; }
        public float Scale { get; set; }

        public Matrix ObjectWorld = Matrix.Identity;

        public Dictionary<string, DrawComponent> DrawComponents { get; set; }
        public Dictionary<string, Entity> Children { get; set; }

        public Entity()
        {
            Position = new Vector3(0f, 0f, 0f);
            Rotation = Matrix.Identity;
            Scale = 1.0f;

            DrawComponents = new Dictionary<string, DrawComponent>();
            Children = new Dictionary<string, Entity>();
        }

        public virtual void Update(GameTime gt, Matrix parentMatrix)
        {
            ObjectWorld = Matrix.CreateScale(Scale) * Rotation * Matrix.CreateTranslation(Position) * parentMatrix;

            foreach (var e in Children.Values)
            {
                e.Update(gt, ObjectWorld);
            }
        }

        public virtual void Draw(BasicEffect effect)
        {
            foreach (var comp in DrawComponents.Values)
            {
                comp.Draw(effect);
            }
            foreach (Entity e in Children.Values)
            {
                e.Draw(effect);
            }
        }
    }
}
